package com.fengwei.security;

import com.fengwei.util.GsonUtil;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Component
public class AjaxAuthenticationFailureHandler implements AuthenticationFailureHandler {
    @Override
    public void onAuthenticationFailure(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AuthenticationException e) throws IOException {

        Map map = new HashMap();
        map.put("result", "SB9999");
        map.put("resultdesc", "SpringSecurity登录校验错误");
        map.put("data", new HashMap<>());

        httpServletResponse.setContentType("text/json;charset=utf-8");
        httpServletResponse.getWriter().write(GsonUtil.toJson(map, false));
    }
}